package com.sxhuayuan.parking.dao;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.sxhuayuan.parking.entity.ParkChannel;
import com.sxhuayuan.parking.entity.ParkingLot;

import java.util.List;

public interface ParkChannelDao extends BaseDao<ParkChannel, Long> {

	/** 根据车场和设备号查询 */
	@Query("select c from ParkChannel c where c.parkingLot.id = ?1 and c.deviceId = ?2")
	ParkChannel findByDeviceId(Long parkingLotId, String deviceId);

	@Query("select c from ParkChannel c where c.parkingLot.id = ?1 and c.channelSn = ?2")
	ParkChannel findByChannelSn(Long lotId, String channelOutSn);

	/** 根据车场删除所有出口 */
	@Modifying
	void deleteByParkingLot(ParkingLot parkingLot);

	ParkChannel findBySn(String sn);

	/** 根据车场查询出口数量 */
	@Query("select count(p.id) from ParkChannel p where p.parkingLot.id in(?1)")
	Long countByParkingLot(List<Long> parkingLotIds);

	/** 根据车场、通道类型、通道名称查询 */
	ParkChannel findByParkingLotAndTypeAndName(ParkingLot parkingLot, ParkChannel.Type type, String name);

	List<ParkChannel> findByParkingLotIdAndType(Long lotId, ParkChannel.Type type);
}
